package com.irdstudio.efp.limit.service.impl;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.basic.framework.core.vo.CusHandOverVO;
import com.irdstudio.efp.limit.service.dao.CusHandOverDao;
import com.irdstudio.efp.limit.service.facade.LmtCusHandOverService;

@Service("lmtCusHandOverService")
public class LmtCusHandOverServiceImpl implements LmtCusHandOverService, FrameworkService {
    
    private static Logger logger = LoggerFactory.getLogger(LmtCusHandOverServiceImpl.class);
    
    @Autowired
    private CusHandOverDao cusHandOverDao;
    
    @Override
    public int doCusHandOver(CusHandOverVO cusHandOverVO) throws Exception {
    	int num = 0;
        logger.info("接收到客户移交请求，入参："+JSONObject.toJSONString(cusHandOverVO));
        try {
            if(Objects.isNull(cusHandOverVO)) {
                throw new Exception("客户移交申请，传入参数为空，请检查！");
            }
            String fromUser = cusHandOverVO.getFromUser();//移出客户经理
            String toUser = cusHandOverVO.getToUser();//接收客户经理
            String fromBrId = cusHandOverVO.getFromBrId();//移出机构
            String toBrId = cusHandOverVO.getToBrId();//接收机构
            List<String> cusIds = cusHandOverVO.getCusIds();//客户号列表
            if(StringUtil.isStrEmpty(fromUser)||StringUtil.isStrEmpty(toUser)
                    ||StringUtil.isStrEmpty(fromBrId)||StringUtil.isStrEmpty(toBrId)||Objects.isNull(cusIds)) {
                throw new Exception("客户移交申请，传入参数：移出客户经理&接收客户经理&移出机构&接收机构&移交客户列表不能为空！");
            }
            
            int count = 0;
            //授信申请表移交
            logger.info("客户移交：开始授信申请表移交.....");
            count = cusHandOverDao.lmtIndivAppCusHandOver(fromUser, toUser, fromBrId, toBrId, cusIds);
            num += count;
            logger.info("客户移交：授信申请表移交记录数：" + count);
            
            //授信协议表移交
            logger.info("客户移交：开始授信协议表移交.....");
            count = cusHandOverDao.lmtPrdContCusHandOver(fromUser, toUser, fromBrId, toBrId, cusIds);
            num += count;
            logger.info("客户移交：授信协议表移交记录数：" + count);
            
            //预授信名单表移交
            logger.info("客户移交：开始预授信名单表移交.....");
            count = cusHandOverDao.lmtPrelistCusHandOver(fromUser, toUser, fromBrId, toBrId, cusIds);
            num += count;
            logger.info("客户移交：预授信名单表移交记录数：" + count);
            
            //额度冻结表记录 
            logger.info("客户移交：开始预额度冻结申请表移交.....");
            count = cusHandOverDao.lmtFreezeAppCusHandOver(fromUser, toUser, fromBrId, toBrId, cusIds);
            num += count;
            logger.info("客户移交：额度冻结申请单表移交记录数：" + count);
            
            //额度解冻表记录 
            logger.info("客户移交：开始预额度解冻申请表移交.....");
            count = cusHandOverDao.lmtUnfreezeAppCusHandOver(fromUser, toUser, fromBrId, toBrId, cusIds);
            num += count;
            logger.info("客户移交：额度解冻申请单表移交记录数：" + count);
            
            //.....
            
            
        } catch (Exception e) {
            logger.error("客户移交出现异常！", e);
            throw e;
        }
        return num;
    }

}
